package com.cims.dao.order;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.cims.common.exception.DaoException;
import com.cims.common.vo.OrderItem;
import com.cims.common.vo.Task;
import com.cims.dao.hibernate.HibernateUtil;

public class OrderItemDAOImpl implements OrderItemDAO{

	@Override
	public List<OrderItem> getListOrder() throws DaoException{
		return HibernateUtil.selectAll(OrderItem.class);
	}

	@Override
	public OrderItem findByOrderItemId(int orderId) throws DaoException{
		return HibernateUtil.findByID(OrderItem.class, orderId);
	}

	@Override
	public List<OrderItem> getListOrderUnAssigned() throws DaoException{
		List<OrderItem> lstVo = null;
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = null;

		try{
			session = factory.openSession();

			Query query = session.createQuery("from OrderItem as o where o.orderId not in (select T.orderItem.orderId from Task as T)");
			lstVo = query.list();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			if(session != null && session.isOpen()){
				session.close();
			}
		}

		return lstVo;
		// from OrderItem as o where o.orderId not in (select T.orderItem.orderId from Task as T)
	}

	@Override
	public void addOrderItem(OrderItem item) throws DaoException{
		HibernateUtil.saveOrUpdate(item);

	}

}
